import cv2 as cv
# 加载图片
imgL = cv.imread('LENA.jpg')
imgR = cv.imread('LENA.jpg')
# 转换为灰度图
grayL = cv.cvtColor(imgL, cv.COLOR_BGR2GRAY)
grayR = cv.cvtColor(imgR, cv.COLOR_BGR2GRAY)
# 提取特征点
fast = cv.FastFeatureDetector_create(50)
kL = fast.detect(grayL, None)
kR = fast.detect(grayR, None)
# 提取描述子
br = cv.BRISK_create()
kL, dL = br.compute(grayL, kL)
kR, dR = br.compute(grayR, kR)

# 创建 BFMatcher 对象
bf = cv.BFMatcher(cv.NORM_L2)
# 根据描述子匹配特征点.
matches = bf.match(dL, dR)
# 画出匹配点
img3 = cv.drawMatches(imgL, kL, imgR, kR, matches, None, flags=2)
cv.imshow("FAST", img3)
# ------------------------------------------------------------------------------------------------
# 初始化Bruteforce匹配器
bf = cv.BFMatcher()
# 通过KNN匹配两张图片的描述子
matches = bf.knnMatch(dL, dR, k=2)
# 筛选比较好的匹配点
good = []
for i, (m, n) in enumerate(matches):
    if m.distance < 0.6 * n.distance:
        good.append(m)
# 画出匹配点
img3 = cv.drawMatches(imgL, kL, imgR, kR, good, None, flags=2)
cv.imshow("FAST-BF", img3)
cv.waitKey(0)
